package com.cf.r944_ak;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class d {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int t = Integer.parseInt(br.readLine());
        while (t-- > 0) {
            String s = br.readLine();
            out.println(solve(s.toCharArray()));
        }
        out.flush();
        out.close();
        br.close();
    }
    public static int solve(char[] s) {
        int z = 0;
        int wei = s[0] - '0';
        int n = s.length;
        int tag = 0;
        for (int i = 1; i < n; i++) {
            if (wei != s[i] - '0') {
                wei = s[i] - '0';
                z++;
            }
            if (s[i - 1] == '0' && s[i] == '1') {
                tag = 1;
            }
        }
        z++;
        return z - tag;
    }

}
